24. Exercise: Private Method
Sphere2
Exercise: Private Method
Abstraction is used to expose only relevant information to the user. By hiding implementation details, we give ourselves flexibility to modify how the program works. In this example, you'll practice abstracting implementation details from the user.
Directions
In this exercise, you'll update the
class Sphere
so that it becomes possible to change the radius of a sphere after it has been initialized. In order to do this, you'll move the two
class invariants
into private member functions.
-
Move the range-check on
radius_into a private member function. -
Move the
volume_calculation, which depends on the value ofradius_into the same private member function. - Verify that the class still functions correctly.
-
Add a mutator method to change the radius of an existing
Sphere. - Verify that the mutator method successfully updates both the radius and the volume.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter
- Opened files (when workspace is loaded): n/a